package com.atXiao.food.chain;

/**
 * 责任链
 *
 * @author: lixiaoxiao
 * @date: 2024/1/12 11:40
 * @version: 1.0
 * @desc:
 */
public abstract class Handler<T> {

    protected Handler nextHandler;

    /**
     * 设置下一个链式节点，用来进行串联
     *
     * @param handler
     */
    public void setNextHandler(Handler handler) {
        nextHandler = handler;
    }

    /**
     * 子类处理逻辑
     */
    public abstract void doHandler(User user);


    public static class Builder<T> {
        private Handler<T> head;
        private Handler<T> tail;

        public Builder<T> addHandler(Handler handler) {
            // 首次添加，首位指向同一个handler
            if (head == null) {
                this.head = this.tail = handler;
                return this;
            }
            // 非首次添加。在末尾连接上handler
            this.tail.setNextHandler(handler);
            this.tail=handler;
            return this;

        }

        /**
         * 返回当前责任链的头部
         * @return
         */
        public Handler<T> build(){
            return this.head;
        }


    }






}
